iT邦幫忙

2023 iThome 鐵人賽

DAY 28
0
SideProject30

三十歲時在做什麼?有沒有空?可以來做遊戲嗎?系列 第 28

[Day 28] RogueLive: VTuber's Dark Odyssey 剛起步就要Debug

  • 分享至 

  • xImage
  •  

昨天在加入player system之後執行出現了錯誤,

error[E0425]: cannot find value `player_movement` in this scope
  --> src/main.rs:44:22
   |
44 |         .add_systems(player_movement.system(), enemy_movement.system())
   |                      ^^^^^^^^^^^^^^^ not found in this scope
   |
note: function `crate::player::player_movement` exists but is inaccessible
  --> src/player.rs:5:1
   |
5  | / fn player_movement(
6  | |     keyboard_input: Res<Input<KeyCode>>,
7  | |     mut query: Query<(&Player, &mut Transform)>
8  | | ) {
...  |
23 | |     }
24 | | }
   | |_^ not accessible

今天要來debug一下,根據錯誤訊息我們可以知道是因為player_movement是私有的,所以要將新增的player.rs給公開,
player.rs

use bevy::prelude::*;

struct Player;

pub fn player_movement(
    keyboard_input: Res<Input<KeyCode>>,
    mut query: Query<(&Player, &mut Transform)>
) {
    for (_player, mut transform) in query.iter_mut() {
        const MOVEMENT_SPEED: f32 = 4.0;
        if keyboard_input.pressed(KeyCode::W) {
            transform.translation.y += MOVEMENT_SPEED;
        }
        if keyboard_input.pressed(KeyCode::S) {
            transform.translation.y -= MOVEMENT_SPEED;
        }
        if keyboard_input.pressed(KeyCode::A) {
            transform.translation.x -= MOVEMENT_SPEED;
        }
        if keyboard_input.pressed(KeyCode::D) {
            transform.translation.x += MOVEMENT_SPEED;
        }
    }
}

struct Enemy;

pub fn enemy_movement(
    mut query: Query<(&Enemy, &mut Transform)>,
    player_query: Query<(&Player, &Transform)>
) {
    if let Some((_player, player_transform)) = player_query.iter().next() {
        for (_enemy, mut enemy_transform) in query.iter_mut() {
            const ENEMY_SPEED: f32 = 2.0;
            let direction = (player_transform.translation - enemy_transform.translation).normalize();
            enemy_transform.translation += direction * ENEMY_SPEED;
        }
    }
}

然後main.rs也要做變動,

//! This example will display a simple menu using Bevy UI where you can start a new game,
//! change some settings or quit. There is no actual game, it will just display the current
//! settings for 5 seconds before going back to the menu.

mod splash;
mod game;
mod menu;
mod player;

use bevy::prelude::*;

const TEXT_COLOR: Color = Color::rgb(0.9, 0.9, 0.9);

// Enum that will be used as a global state for the game
#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
enum GameState {
    #[default]
    Splash,
    Menu,
    Game,
}

// One of the two settings that can be set through the menu. It will be a resource in the app
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
enum DisplayQuality {
    Low,
    Medium,
    High,
}

// One of the two settings that can be set through the menu. It will be a resource in the app
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
struct Volume(u32);

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // Insert as resource the initial value for the settings resources
        .insert_resource(DisplayQuality::Medium)
        .insert_resource(Volume(7))
        // Declare the game state, whose starting value is determined by the `Default` trait
        .add_state::<GameState>()
        .add_systems(Startup, setup)
        .add_systems(player:player_movement.system(), player:enemy_movement.system())
        // Adds the plugins for each state
        .add_plugins((splash::SplashPlugin, menu::MenuPlugin, game::GamePlugin))
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2dBundle::default());
}

// Generic system that takes a component as a parameter, and will despawn all entities with that component
fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
    for entity in &to_despawn {
        commands.entity(entity).despawn_recursive();
    }
}

這樣就解決了這個錯誤了


上一篇
[Day 27] RogueLive: VTuber's Dark Odyssey 實作玩家與角色
下一篇
[Day 29] RogueLive: VTuber's Dark Odyssey : 難道要做不出來了嗎
系列文
三十歲時在做什麼?有沒有空?可以來做遊戲嗎?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言